#!/bin/bash

# 快速修复Docker Compose配置文件

echo "🔧 修复Docker Compose配置文件..."

# 备份原文件
cp docker-compose.yml docker-compose.yml.backup
cp docker-compose.prod.yml docker-compose.prod.yml.backup

echo "✅ 已备份原配置文件"

# 修复开发环境配置
cat > docker-compose.yml << 'EOF'
services:
  netease-music-api:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: netease-music-api
    restart: unless-stopped
    ports:
      - "3000:3000"
    environment:
      - NODE_ENV=production
      - PORT=3000
    # 如果需要数据持久化，取消注释下面的volumes配置
    # volumes:
    #   - ./data:/app/data
    #   - ./config:/app/config
    networks:
      - netease-api-network
    healthcheck:
      test: ["CMD", "node", "-e", "require('http').get('http://localhost:3000/banner', (res) => { process.exit(res.statusCode === 200 ? 0 : 1) })"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.netease-api.rule=Host(`your-domain.com`)"
      - "traefik.http.routers.netease-api.tls=true"
      - "traefik.http.services.netease-api.loadbalancer.server.port=3000"

  # 可选：添加Nginx反向代理
  nginx:
    image: nginx:alpine
    container_name: netease-api-nginx
    restart: unless-stopped
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf:ro
      - ./ssl:/etc/nginx/ssl:ro
    depends_on:
      - netease-music-api
    networks:
      - netease-api-network
    profiles:
      - nginx

networks:
  netease-api-network:
    driver: bridge
EOF

# 修复生产环境配置
cat > docker-compose.prod.yml << 'EOF'
services:
  netease-music-api:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: netease-music-api
    restart: unless-stopped
    environment:
      - NODE_ENV=production
      - PORT=3000
    # 如果需要数据持久化，取消注释下面的volumes配置
    # volumes:
    #   - ./data:/app/data
    networks:
      - netease-api-network
    healthcheck:
      test: ["CMD", "node", "-e", "require('http').get('http://localhost:3000/banner', (res) => { process.exit(res.statusCode === 200 ? 0 : 1) })"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s
    deploy:
      resources:
        limits:
          memory: 512M
          cpus: '0.5'
        reservations:
          memory: 256M
          cpus: '0.25'

  nginx:
    image: nginx:alpine
    container_name: netease-api-nginx
    restart: unless-stopped
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf:ro
      - ./ssl:/etc/nginx/ssl:ro
      - nginx-logs:/var/log/nginx
    depends_on:
      - netease-music-api
    networks:
      - netease-api-network
    deploy:
      resources:
        limits:
          memory: 128M
          cpus: '0.2'
        reservations:
          memory: 64M
          cpus: '0.1'

volumes:
  nginx-logs:

networks:
  netease-api-network:
    driver: bridge
EOF

echo "✅ 配置文件已修复"

# 验证配置
echo "🔍 验证配置文件..."
if docker-compose -f docker-compose.yml config > /dev/null 2>&1; then
    echo "✅ docker-compose.yml 配置正确"
else
    echo "❌ docker-compose.yml 配置仍有问题"
    docker-compose -f docker-compose.yml config
fi

if docker-compose -f docker-compose.prod.yml config > /dev/null 2>&1; then
    echo "✅ docker-compose.prod.yml 配置正确"
else
    echo "❌ docker-compose.prod.yml 配置仍有问题"
    docker-compose -f docker-compose.prod.yml config
fi

echo "🎉 修复完成！"
echo "💡 如果需要恢复原配置，请运行: cp docker-compose.yml.backup docker-compose.yml" 